package com.example.emb.service.common;

import ch.qos.logback.core.net.server.Client;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.emb.service.exception.SmsErrorException;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AliService {
    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

//    @Value("${aliyun.sms.signName}")
//    private String signName;
//
//    @Value("${aliyun.sms.templateCode}")
//    private String templateCode;

    public void smsMessage(String telephone, int code){  //AccessKey ID 账号 AccessKey secret密码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        //阿里云原有的配置信息我们不需要改动
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //自定义短信信息
        request.putQueryParameter("PhoneNumbers", telephone); //发送给哪个手机号
        request.putQueryParameter("SignName", "EmbManage");  //自己配置的短信签名
        request.putQueryParameter("TemplateCode", "SMS_465695089"); //自己配置的模板 模版CODE

        //构建一个短信验证码
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));   //转换成json字符串
        try {
            CommonResponse response = client.getCommonResponse(request); //发送至客户端
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

//    public void sendSms(String phoneNumber, String templateParam) {
//        try {
//            // 初始化SDK
//            DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
//            IAcsClient client = new DefaultAcsClient(profile);
//
//            // 构造请求
//            SendSmsRequest request = new SendSmsRequest();
//            request.setPhoneNumbers(phoneNumber);
//            request.setSignName(signName);
//            request.setTemplateCode(templateCode);
//            request.setTemplateParam(templateParam);
//
//            // 发送短信
//            SendSmsResponse response = client.getAcsResponse(request);
//
//            // 处理响应结果
//            if ("OK".equals(response.getCode())) {
//                System.out.println("短信发送成功！");
//            } else {
//                System.err.println("短信发送失败：" + response.getMessage());
//                throw new SmsErrorException("短信发送失败");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}

